<!--  -->
<template>
  <div>
    <div v-if="requestError" class="text-c errorPlace">
      <img width="100px" height="100px" :src="require('@/assets/images/pic_neterror.png')" alt="获取数据失败" />
      <div class="color-b font14" style="margin: 10px 0 0 0">
        获取数据失败!
      </div>
      <van-button plain round  size="small" color="#7232dd" type="primary" @click="getdetail">点击重试</van-button>
    </div>
    <van-form v-else @submit="submitFromData">
      <van-tabs v-model="active" color="#1989fa" sticky animated line-width="33%" offset-top="50" @click="onClick">
        <van-tab title="项目信息">
          <tabone ref="toMsgs" :titledata='titledata' :businessdata='businessdata' :fromdata='fromdata' :showHistory='showHistory' :approveList='approveList' :historydata='historydata' :canUpFiles="canUpFiles"/>
        </van-tab>
        <van-tab title="其他信息">
          <tabtwo :fromdata='fromdata'/>
        </van-tab>
      </van-tabs>
      <div class="left_botton" v-if="showMsgBox">
        <button block type="button" @click="toMsgValue()"
          style="color:#4A90E5; display: inline-block;width:100%;border: 0px solid #4A90E5;border-radius: 15px 0px 0px 15px;
          padding:10px 10px 10px 15px;font-size:13px">意见</button>
      </div>
      <div v-if="todotype==true||toread==true"  class="footer">
        <van-button block type="info" native-type="submit" :disabled="canToNext==true?false:true" :color="canToNext ? '#4A90E5' : '#cccccc'" :style="{display: 'inline-block',width: '100%',border: canToNext ? '0px solid #4A90E5' : '0px solid #cccccc'}">{{toNextValue}}</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import {
  getMIFormDetail,
  queryProjectContractAssetsById,
  getWfLinkTracks,
  getWfOptMags
} from '@/api/dictapi'
import util from "@/assets/js/public";
import tabone from "@/views/htbdetail/tabone"
import tabtwo from "@/views/htbdetail/tabtwo"
import { mapGetters } from "vuex"
import { Notify, Toast } from 'vant'

export default {
  data() {
    return {
      active: 0,
      requestError:false,
      params: this.$route.query,
      showHistory: false,
      historydata: {},//已办显示当前办理人
      fromdata: {},//收入确认信息
      approveList: [],//审批意见列表
      titledata:{}, //主单据信息
      businessdata:{}, //商机基础信息
      showMsgBox:true,
      toNextValue:'流程下一步',
      canToNext: true,
      todotype:false,
      toread:false,
      canUpFiles:false,
    }
  },
  computed: {
    ...mapGetters(['user']),
    ...mapGetters(['userinfo'])
  },
  //生命周期--创建完成
  created() {
    if(this.params.pmInsType==null&&this.params.processType!=null) {
      this.params.pmInsType = this.params.processType
    }
    if (this.params.todoType % 2 == 0) {
      this.showHistory = true;
    }
    if(this.params.todoType=='1'){
      this.todotype=true;
      if(this.params.location=='dict.start'){
        this.canToNext=false
        this.toNextValue='暂不支持手机端，请至PC办理'
        this.canUpFiles=false
      }else {
        this.canToNext=true
        this.toNextValue='流程下一步'
        this.canUpFiles=true
      }
    }else if(this.params.todoType=='3'){
      this.toread=true;
    }
    this.getdetail();
  },
  //生命周期--挂载完成
  components: {
    tabone,
    tabtwo,
  },
  //方法调用
  methods: {
    onClick(event) {
      console.log("切换tabs:", event)
        //对name进行赋值，此处name值取tabs中集合数据的位置顺序值
      if (event==0){
        this.showMsgBox=true
      }else {
        this.showMsgBox=false
      }
    },
    toMsgValue(){
       this.$refs.toMsgs.tomsg()
    },
    goBack() {
      this.$router.back(-1) //返回上一页
    },
    onError(result){
      this.requestError=true;
      Toast.clear();
      Notify(result.message);
    },
    getdetail() {
      Toast.loading({
        message: '加载中',
        forbidClick: false
      });

      getMIFormDetail(Object.assign(this.params, { userName: this.user.userName }))
        .then((result) => {
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.titledata = result.data.data
            this.params.pmInsId = result.data.data.pmInsId;
            this.queryProjectContractAssetsById();//获取第一步信息
          }

        })
    },
    //获取两个number code
    queryProjectContractAssetsById() {
      queryProjectContractAssetsById(this.params,this.titledata.projectInformationId)
        .then((result) => {
          Toast.clear();
          if (!result.data) {
            this.onError(result);
            return;
          }
          if (result.data.data) {
            this.fromdata = result.data.data;
          }
          if (this.showHistory==true) {
            this.getWfLinkTracks();
          }else {
            this.getWfOptMags()
          }
        })
    },
    getWfLinkTracks() {
      getWfLinkTracks(Object.assign(this.params),{userName:this.user.userName})
        .then((result)=>{
          Toast.clear();
          if(!result.data){
            Notify(result.message);
            return;
          }
          if(result.data.data){
            //数据过滤，并生成新的数组
            this.historydata=result.data.data.filter(item=>item.CURRENTSTATE==10)[0]
            if(this.historydata){
                if(this.historydata.MANAGER){
                  this.showHistory=true;
                }else{
                  this.showHistory=false;
                }
            }else{
              this.showHistory=false;
            }
          }else{
            this.showHistory=false;
          }
          this.getWfOptMags()
        })
    },

    getWfOptMags() {
      getWfOptMags(Object.assign(this.params), { userName: this.user.userName })
        .then((result) => {
          if (!result.data) {
            Notify(result.message);
            return;
          }
          this.approveList = result.data.data.reverse();
        })
    },
    submitFromData() { //提交流转
      this.params.formData=this.fromdata;
      var formSendData={"htmlParamMap":{}};
      formSendData.htmlParamMap = this.fromdata;
      formSendData.id = this.titledata.id;
      formSendData.pmInsId = this.titledata.pmInsId;
      formSendData.processType = this.params.pmInsType
      formSendData.workItemId = this.params.workItemId;
      console.log("传给递交页面:", this.params)
      var params1 = {
        kv: {
          currentUserCode: util.encrypt(this.params.userName),
          source: 'MOBILE',
          location: this.params.location,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          processType: this.params.pmInsType,
          isClaimFun:'3',
          isClaimCounty:'3',
          isOperateCity:'3',
          isOperateCounties:'3',
          isProvinceConfirm:'3',
          isCityConfirm:'3',
          isConverBusiness: this.fromdata.isConverBusiness,
          preAmount: this.fromdata.preAmount,
          productManagerFlag: this.fromdata.productManagerFlag,
        },
        body:{
          location: this.params.location,
          processInstId: this.params.processInstId,
          processDefName: this.params.processDefName,
          source: 'MOBILE',
        }
      };
      var params2 = {
        kv: {
          "currentUserCode": util.encrypt(this.params.userName),
          "source": 'MOBILE',
          "location": this.params.location,
          "processInstId": this.params.processInstId,
          'processType': this.params.pmInsType,
          'receiptCode': this.params.processInstId
        },
      };
      var params3 = {
        kv: {
          "source": 'MOBILE',
          "currentUserCode": util.encrypt(this.params.userName),
          "workItemId": this.params.todoType==3?"":this.params.workItemId,
          "processInstId": this.params.processInstId,
          "location": this.params.location,
          "notificationId":this.params.todoType==3?this.params.wfnid:""
        },
      };
      localStorage.setItem('submitPageParam', JSON.stringify({
        getDecisionsUrl:'decisionAndUser$api$getDecisions',
        getDecisionsParams:params1,
        getUserUrl:'decisionAndUser$api$getOrgAndUser',
        getUserParams:params2,
        tosubmintUrl:'applicationFormProject$api$startSubmitProcess',
        tosubmintParams:params3,
        formData:formSendData,
        fromMoa:this.params.fromMoa,
        itemId:this.params.itemId,
        type:this.params.todoType==3?"toRead":"toDo"
      }));
      this.$router.push(
          {
            name: 'srqrsubmit'
          }
      );
    },

  }
}
</script>
<style  scoped>
.errorPlace{
  position:absolute;
  margin-top: -80px;
  top: 50%;
  margin-left: -50px;
  left: 50%;
}
</style>